package com.qianxunclub.chapter8.interceptor;


import com.qianxunclub.chapter8.annotation.GetData;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @author zhangbin
 */
@Slf4j
@Component
public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            GetData getData = AnnotationUtils.findAnnotation(method, GetData.class);
            if(getData != null){
                log.info("GetData:" + method.getName());
            }
        }
        super.postHandle(request, response, handler, modelAndView);
    }
}
